package com.lkd.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.dao.SkuDao;
import com.lkd.entity.SkuClassEntity;
import com.lkd.entity.SkuEntity;
import com.lkd.service.SkuClassService;
import com.lkd.service.SkuService;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SkuServiceImpl extends ServiceImpl<SkuDao, SkuEntity> implements SkuService {

    @Autowired
    private SkuClassService skuClassService;

    /*
    * 商品分页
    * */
    @Override
    public Pager<SkuEntity> search(Integer pageIndex, Integer pageSize, String skuName, Long classId) {
        //1. 条件+分页查询商品基本信息
         //1-1 分页
        Page<SkuEntity> page = new Page<>(pageIndex, pageSize);
        //1-2 条件
        LambdaQueryWrapper<SkuEntity> qw = new LambdaQueryWrapper<>();
        //名称
        qw.like(StrUtil.isNotEmpty(skuName),SkuEntity::getSkuName,skuName);
        //类别
        qw.eq(ObjectUtil.isNotEmpty(classId),SkuEntity::getClassId,classId);
        //1-3 查询
        this.page(page,qw);
        //2.遍历商品list，填充商品所属的分类
//        for (SkuEntity skuEntity : page.getRecords()) {
//            //查询分类
//            SkuClassEntity skuClassEntity = skuClassService.getById(skuEntity.getClassId());
//            //填充
//            skuEntity.setSkuClass(skuClassEntity);
//        }
        //3.返回分页对象
        return Pager.build(page);
    }

    @Override
    public Boolean saveInfo(SkuEntity skuEntity) {
        return this.save(skuEntity);
    }
}
